#!/usr/bin/python
# -*- coding: UTF-8 -*-

import urequests as requests
import mymd5 as md5
import utime as time
import uio
from machine import RTC

def wx_get_req(url, params):
    params['timestamp'],params['sign'] = sign(params['secretkey'])
    params.pop('secretkey')
    return requests.get(url, params)

def wx_post_req(url, params, img=None, bodyStr=None):
    ret = file_get_contents(img) if img else bodyStr
    params['timestamp'],params['sign'] = sign(params['secretkey'])
    params.pop('secretkey')
    print(params)
    return requests.post(url, params=params, data=ret)

#img to str
def file_get_contents(filename, use_include_path = 0, context = None, offset = -1, maxlen = -1):
    fp = uio.open(filename,'rb')
    try:
        if (offset > 0):
            fp.seek(offset)
        ret = fp.read(maxlen)
        return ret
    finally:
        fp.close( )

#sign
def sign(secretkey):
    #m = hashlib.md5()
    nowTime = int((time.time() -28800)* 1000)
    before =  secretkey + str(nowTime)
    #m.update(before.encode('utf8'))
    m = md5.init_mess(before.encode('utf8'))
    return nowTime, m

